﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HZ.MBSM.DBUtility;

public partial class Supervise_TraSort : System.Web.UI.Page
{
    public string _strEndDate, _strStartDate, _AreaId = "", _strUserType = "", _strAccountId_YY = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        string strSql;

        System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> _AccountType = new System.Collections.Generic.List<HZ.MBSM.DAL.AccountType>();
        _AccountType.Add(HZ.MBSM.DAL.AccountType.enumHospitale);
        _AccountType.Add(HZ.MBSM.DAL.AccountType.enumAttorney);
        _AccountType.Add(HZ.MBSM.DAL.AccountType.enumSupervise);
        if (HZ.MBSM.BLL.Login.CheckAssert(_AccountType))
        {
            HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
            if (_Account != null)
            {
                _AreaId = SysFun.ToTrim(_Account.AreaId);
                _strUserType = SysFun.ToTrim(_Account.Type);
                _strAccountId_YY = SysFun.ToTrim(_Account.AccountId);

            }
            if (!IsPostBack)
            {
                //<!--初始化统计时间
                int Year, Month, Day, DMonth, Year1;
                Year = DateTime.Now.Year;
                Year1 = Year;
                DMonth = DateTime.Now.Month;
                Month = DMonth - 1;
                if (Month == 0)
                {
                    Year1 -= 1;
                    Month = 12;
                }

                Day = DateTime.Now.Day;
                txtStartDate.Text = Year1.ToString() + "-" + Month.ToString() + "-01";
                txtEndDate.Text = Year.ToString() + "-" + DMonth.ToString() + "-01";

                _strEndDate = SysFun.IsSQL(SysFun.ToTrim(Request.QueryString["EndDate"]));
                _strStartDate = SysFun.IsSQL(SysFun.ToTrim(Request.QueryString["StartDate"]));
                if (_strEndDate != "")
                {
                    txtEndDate.Text = _strEndDate;
                }
                if (_strStartDate != "")
                {
                    txtStartDate.Text = _strStartDate;
                }
                //初始化统计时间-->

                //<!--添加地区
                int i;
                Db DbProject = new Db();
                System.Data.IDataReader dr;
                if (_AreaId == "530000")
                {
                    i = 0;
                   
                    strSql = "select * from area where areaid in (select areaid from hospital) ";
                    dr = DbProject.DataReader(strSql);
                    lstAreaId.Items.Add("全部");
                    while (dr.Read())
                    {
                        lstAreaId.Items.Add(dr["areaname"].ToString());
                        lstAreaId.Items[++i].Value = dr["areaid"].ToString();

                    }
                    lstAreaId.DataBind();
                    dr.Close();
                    dr.Dispose();
                  
                    lstAreaId.Visible = true;
                    Label1.Visible = true;
                    string tempAreaId = SysFun.ToTrim(Request.QueryString["AreaId"]);
                    if (tempAreaId != "")
                    {
                        lstAreaId.SelectedValue = tempAreaId;
                    }
                }
                else
                {
                    lstAreaId.Visible = false;
                    Label1.Visible = false;
                }
                //添加地区-->

                //<!--初始化大类
                i = 0;
                string _strSortId = SysFun.ToTrim(Request.QueryString["SortId"]);
                if (_strSortId != "")
                {
                    strSql = "select sortid,sortname from product_sort where ( SortId=" + _strSortId + " or FrontSortId=" + _strSortId + " )";
                    dr = DbProject.DataReader(strSql);
                    while (dr.Read())
                    {
                        lstSort.Items.Add(dr["sortname"].ToString());
                        lstSort.Items[i++].Value = dr["sortid"].ToString();
                    }
                    lstSort.DataBind();
                    dr.Close();
                    dr.Dispose();
                    lstSort.SelectedValue = _strSortId;
                }
                else
                {
                    strSql = "select sortid,sortname from product_sort ";
                    dr = DbProject.DataReader(strSql);
                    while (dr.Read())
                    {
                        lstSort.Items.Add(dr["sortname"].ToString());
                        lstSort.Items[i++].Value = dr["sortid"].ToString();
                    }
                    lstSort.DataBind();
                    dr.Close();
                    dr.Dispose();
                }
                //初始化大类-->
                DbProject.Close();
            }

            _strEndDate = txtEndDate.Text.ToString();
            _strStartDate = txtStartDate.Text.ToString();
            if (_strStartDate != "" && _strEndDate != "")
            {
                if (Request.Form["butSelectAll"] == "查看全部")
                {

                    strSql = @"select  SortId,sortname,convert(numeric(20,2),sum(GoodsNum*TradePrice)) as DGJE ,convert(numeric(20,2),sum(inflownum*TradePrice)) as DHJE
                                ,convert(numeric(20,2),sum(case when GoodsState=0 then inflownum*TradePrice else 0 end)) as YZB,convert(numeric(20,2),sum(case when GoodsState=1 then inflownum*TradePrice else 0 end)) as BACG,convert(numeric(20,2),SUM(case when GoodsState=2 then inflownum*TradePrice else 0 end)) AS BXJ                                  
                                from tra_GoodsId where  {0}  group by SortId,sortname  Order  by DHJE DESC,SortId";
                    string _strtempWhere = fctConditions();
                    strSql = String.Format(strSql, _strtempWhere);
                    Db db1 = new Db();
                    System.Data.IDataReader dr;
                    dr = db1.DataReader(strSql);
                    GridView1.DataSource = dr;
                    GridView1.DataBind();
                    dr.Close();
                    dr.Dispose();
                    db1.Close();
                }
                else
                {
                    showGridView();
                }
            }
        }
    }
    protected void Button5_Click(object sender, EventArgs e)
    {

    }

    /// <summary>
    /// 数据绑定
    /// </summary>
    public void showGridView()
    {

        string strConditions = fctConditions();
        Db db = new Db();
        DbPage dbPage = new DbPage(db);
        dbPage.Table = "tra_GoodsId";                                               //要查询的表明
        dbPage.SelectFieldGroup = "SortId,sortname";
        dbPage.Key = "SortId";                                               //表的主键    
        dbPage.Where = strConditions;                                           //查询条件    
        dbPage.SelectField = "SortId,sortname,convert(numeric(20,2),sum(GoodsNum*TradePrice)) as DGJE ,convert(numeric(20,2),sum(inflownum*TradePrice)) as DHJE ,convert(numeric(20,2),sum(case when GoodsState=0 then inflownum*TradePrice else 0 end)) as bidprice,convert(numeric(20,2),sum(case when GoodsState=1 then inflownum*TradePrice else 0 end)) as bakeprice,convert(numeric(20,2),SUM(case when GoodsState=2 then inflownum*TradePrice else 0 end)) AS Nolimtprice,convert(numeric(20,2),SUM(case when GoodsState=3 then inflownum*TradePrice else 0 end)) AS GMPprice,convert(numeric(20,2),SUM(case when GoodsState=4 then inflownum*TradePrice else 0 end)) AS KeyPrice";//要显示的字段
        dbPage.PageIndex = SysFun.ToInt(Request["CurrentlyPageIndex"]);          //要显示第几页   
        dbPage.PageSize = 30;                                                    //没有显示记录条数   
        dbPage.OrderBy = "DHJE,SortId";                  //排序字段
        dbPage.OrderByIsDesc = true;
        dbPage.DGJE = "convert(numeric(20,2),sum(GoodsNum*TradePrice)) as DGJE";
        dbPage.DHJE = "convert(numeric(20,2),sum(inflownum*TradePrice)) as DHJE";
        System.Data.IDataReader dr = dbPage.ExecuteReaderGroup(60);
        GridView1.DataSource = dr;
        GridView1.DataBind();
        dr.Close();
        dr.Dispose();
        dbPage.Db.Close();
        db.Close();
        this.DbPageFlip1.DbPage = dbPage;//将数据传给控键



    }
    /// <summary>
    /// 生成查询条件
    /// </summary>
    /// <returns></returns>
    public string fctConditions()
    {
        string strtemp = " DealStatus=2 and", _strFrontSortId;
        _strEndDate = txtEndDate.Text.ToString();
        _strStartDate = txtStartDate.Text.ToString();
        _strFrontSortId = SysFun.ToTrim(Request.QueryString["FrontSortId"]);
        if (_strFrontSortId != "")
        {
            strtemp += "  FrontSortId=" + _strFrontSortId + " and";
        }

        if (lstAreaId.Items.Count > 0)
        {
            if (lstAreaId.SelectedValue != "全部")
            {
                _AreaId = lstAreaId.SelectedValue;
            }
        }

        if (_strEndDate != "" && _strStartDate != "")
        {
            strtemp += " hospitalinflowdate >='" + _strStartDate + "' and hospitalinflowdate <='" + _strEndDate + "' and";
        }
        if (_strUserType == "3")
        {
            strtemp += "  AccountId_YY=" + _strAccountId_YY + " and";
        }
        else
        {
            strtemp += SysFun.AreaIdWhere(_AreaId);

        }
        if (strtemp != "")
        {
            strtemp = "  " + strtemp.Substring(0, strtemp.Length - 3);
        }
        return strtemp;
    }

}
